<?php
namespace app\client\lib;
class Request {
    private $client = null;
    private $host = ['106.14.135.226'];
    private $port = 7777;
    private $header = ['Content-Type' => 'application/json'];
    private $setOption = ['timeout' => 60];
    
    public function __construct($gatewayNo) {
        $this->client = new \Swoole\Coroutine\Http\Client($this->host[$gatewayNo - 1], $this->port);
    }
    
    
    public function close() {
        $this->client->close();
    }
    
    public function body() {
        return $this->client->body;
    }
    
    public function send($url, $data) {
        if(is_array($data)) {
            $data = json_encode($data);
        }
        
        $this->client->setHeaders($this->header);
        $this->client->set($this->setOption);
        
        $this->client->post('/sendGateway/' .  $url, $data);
        $body = $this->client->body;
        if(strtolower($body) === 'error'){
            return false;
        }elseif( $body === ''){
            return false;
        }
        
        $body = json_decode($body, true);
        if($body == null){
           return false;
        }
        
        return $body['statuscode'] == 200;
    }
}